ApplicationServiceDefinition = function(){
    this.timeout = 6000000;
}
ApplicationServiceDefinition.prototype = {
    setTimeout: function(timeout){
        this.timeout = timeout;
    }
    ,
    RefreshState: function(args, successHandler, errorHandler, scope){
        return Ext.Ajax.request({
            url: 'ApplicationService/RefreshState'
            // url: 'DotExeProxy.ashx/ApplicationService/RefreshState'

            ,
            timeout: this.timeout
            ,
            method: 'POST'
            ,
            jsonData: {
                args: args
            }

            ,
            success: function(res, options){
                if(!successHandler) return;
                var obj = getObject(res.responseText);
                successHandler.apply(this, [obj, this, options, res]);
            }
            ,
            failure: function(res, options){
                var obj = null;
                if(res.status == -1){
                    obj = {
                        Message: res.statusText
                        ,
                        Timeout: true
                        ,
                        ExceptionType: 'timeout'
                        ,
                        InnerMessage: ''
                        ,
                        StackTrace: ''
                    }
                }
                else{
                    obj = getObject(res.responseText);
                }

                obj = getErrorObject(obj);

                if(ApplicationService.defaultErrorHandler)
                    ApplicationService.defaultErrorHandler.apply(this, [obj, this, options, res]);
                else if(errorHandler)
                    errorHandler.apply(this, [obj, this, options]);
            }
            ,
            scope:scope
        });
    }
    , BatchRefreshState: function(argsArray, successHandler, errorHandler, scope){
        return Ext.Ajax.request({
                url: 'ApplicationService/BatchRefreshState'
                , timeout: this.timeout
                , method: 'POST'
                , jsonData: {argsArray: argsArray}

        , success: function(res, options){
            if(!successHandler) return;
            var obj = getObject(res.responseText);
            successHandler.apply(this, [obj, this, options, res]);
        }
        , failure: function(res, options){
            var obj = null;
            if(res.status == -1){
                obj = {
                    Message: res.statusText
                    , Timeout: true
                    , ExceptionType: 'timeout'
                    , InnerMessage: ''
                    , StackTrace: ''
                }
            }
            else{
                 obj = getObject(res.responseText);
            }
            
            obj = getErrorObject(obj);
            
            if(ApplicationService.defaultErrorHandler)
                ApplicationService.defaultErrorHandler.apply(this, [obj, this, options, res]);
            else if(errorHandler)
                errorHandler.apply(this, [obj, this, options]);
        }
        , scope:scope
        });
}
    ,
    Init: function(args, successHandler, errorHandler, scope){
        return Ext.Ajax.request({
            url: 'ApplicationService/LoadUseCase'
            // url: 'DotExeProxy.ashx/ApplicationService/Init'

            ,
            timeout: this.timeout
            ,
            method: 'POST'
            ,
            jsonData: {
                args: args
            }

            ,
            success: function(res, options){
                if(!successHandler) return;
                var obj = getObject(res.responseText);
                successHandler.apply(this, [obj, this, options, res]);
            }
            ,
            failure: function(res, options){
                var obj = null;
                if(res.status == -1){
                    obj = {
                        Message: res.statusText
                        ,
                        Timeout: true
                        ,
                        ExceptionType: 'timeout'
                        ,
                        InnerMessage: ''
                        ,
                        StackTrace: ''
                    }
                }
                else{
                    obj = getObject(res.responseText);
                }

                obj = getErrorObject(obj);

                if(ApplicationService.defaultErrorHandler)
                    ApplicationService.defaultErrorHandler.apply(this, [obj, this, options, res]);
                else if(errorHandler)
                    errorHandler.apply(this, [obj, this, options]);
            }
            ,
            scope:scope
        });
    }
    ,
    RemoveController: function(ControllerElementId, successHandler, errorHandler, scope){
        return Ext.Ajax.request({
            url: 'ApplicationService/RemoveController'
            ,
            timeout: this.timeout
            ,
            method: 'POST'
            ,
            jsonData: {
                ControllerElementId: ControllerElementId
            }

            ,
            success: function(res, options){
                if(!successHandler) return;
                var obj = getObject(res.responseText);
                successHandler.apply(this, [obj, this, options, res]);
            }
            ,
            failure: function(res, options){
                var obj = null;
                if(res.status == -1){
                    obj = {
                        Message: res.statusText
                        ,
                        Timeout: true
                        ,
                        ExceptionType: 'timeout'
                        ,
                        InnerMessage: ''
                        ,
                        StackTrace: ''
                    }
                }
                else{
                    obj = getObject(res.responseText);
                }

                obj = getErrorObject(obj);

                if(ApplicationService.defaultErrorHandler)
                    ApplicationService.defaultErrorHandler.apply(this, [obj, this, options, res]);
                else if(errorHandler)
                    errorHandler.apply(this, [obj, this, options]);
            }
            ,
            scope:scope
        });
    }
    ,
    GetUseCases: function(successHandler, errorHandler, scope){
        return Ext.Ajax.request({
            url: 'ApplicationService/GetUseCases'
            ,
            timeout: this.timeout
            ,
            method: 'POST'
            ,
            jsonData: {}

            ,
            success: function(res, options){
                if(!successHandler) return;
                var obj = getObject(res.responseText);
                successHandler.apply(this, [obj, this, options, res]);
            }
            ,
            failure: function(res, options){
                var obj = null;
                if(res.status == -1){
                    obj = {
                        Message: res.statusText
                        ,
                        Timeout: true
                        ,
                        ExceptionType: 'timeout'
                        ,
                        InnerMessage: ''
                        ,
                        StackTrace: ''
                    }
                }
                else{
                    obj = getObject(res.responseText);
                }
                obj = getErrorObject(obj);

                if(ApplicationService.defaultErrorHandler)
                    ApplicationService.defaultErrorHandler.apply(this, [obj, this, options, res]);
                else if(errorHandler)
                    errorHandler.apply(this, [obj, this, options]);
            }
            ,
            scope:scope
        });
    }
}
var ApplicationService = new ApplicationServiceDefinition();
function getObject(data) {
    var obj = null;
    if (data) {
        data = data.replace(new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\"', 'g'), "$1new Date($2)");
        try {
            obj = eval('(' + data + ')');
        }
        catch (e) {
            alert("Error evaluating data: " + data);
        }
    }
    return obj;
}

function getErrorObject(err) {
    return {
        errorObject: err
        ,
        get_message: function() {
            return err.Message;
        }
        ,
        get_stackTrace: function() {
            return err.StackTrace;
        }
        ,
        get_statusCode: function() {
            return -1;
        }
        ,
        get_exceptionType: function() {
            return err.ExceptionType;
        }
        ,
        get_timedOut: function() {
            return err.Timeout;
        }
    }
}